﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Drawing;
using System.Windows.Media;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Windows.Media.Imaging;
using PracticalExamples.Util;

namespace PracticalExamples.GDI绘制文字
{
    /// <summary>
    /// DrawingFont.xaml 的交互逻辑
    /// </summary>
    public partial class DrawingFont : Window
    {
        private FontItem fontItem;

        public DrawingFont()
        {
            InitializeComponent();
            InitFontName();
            numFontSize.Text = "12";
        }

        private void InitFontName()
        {
            try
            {
                InstalledFontCollection insFont = new InstalledFontCollection();
                foreach (var family in insFont.Families)
                {
                    cbFontName.Items.Add(family.Name);
                }

                cbFontName.SelectedIndex = 0;
            }
            catch (Exception e)
            {

                Console.WriteLine("ERROR:203 加载本地字体失败！message:[{0}]", e.Message);
            }
        }

        private void GetFontItem(string text)
        {
            if (text.Length<=0)
            {
                fontItem = null;
                return;
            }
            fontItem = new FontItem();
            fontItem.FontColor = ((SolidColorBrush)rFontColor.Fill).Color;
            fontItem.FontName = cbFontName.Text;
            fontItem.FontSize = "".Equals(numFontSize.Text) ? 1 : int.Parse(numFontSize.Text);
            fontItem.Text = text;
        }

        private void DrawText()
        {
            try
            {
                if (fontItem==null)
                {
                    imgFont.Source = null;
                    return;
                }

                Font fontText = new Font(fontItem.FontName, fontItem.FontSize);
                System.Drawing.Size sizeText = System.Windows.Forms
                                                .TextRenderer.MeasureText(fontItem.Text, fontText, new System.Drawing.Size(0, 0), 
                                                                          System.Windows.Forms.TextFormatFlags.NoPadding);
                Rect viewport = new Rect(0, 0, sizeText.Width, sizeText.Height);

                if (viewport.Width == 0 || viewport.Height==0)
                {
                    return;
                }
                Bitmap tempMap = new Bitmap((int)viewport.Width, (int)viewport.Height);
                Graphics graphics = Graphics.FromImage(tempMap);
                //平滑模式
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //文本渲染器提示 =网格拟合
                graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                //合成质量
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                RectangleF rectF = new RectangleF(0, 0, sizeText.Width, sizeText.Height);
                GraphicsPath path = new GraphicsPath();
                path.AddString(fontItem.Text, fontText.FontFamily, 
                    (int)fontText.Style, fontText.Size, rectF, 
                    System.Drawing.StringFormat.GenericDefault);
                graphics.FillPath(new SolidBrush(System.Drawing.Color.FromArgb(fontItem.FontColor.A, fontItem.FontColor.R, fontItem.FontColor.G, fontItem.FontColor.B)),path);

                path.Dispose();

                BitmapImage tempImage = ImageHelper.BitmapToBitmapImage(tempMap, System.Drawing.Imaging.ImageFormat.Png);

                graphics.Dispose();
                tempMap.Dispose();

                if(tempImage != null)
                {
                    imgFont.Source = tempImage;
                    imgFont.Width = tempImage.Width;
                    imgFont.Height = tempImage.Height;
                    //设置显示的位置
                    Canvas.SetLeft(imgFont, (mainCanvas.ActualWidth - tempImage.Width) / 2);
                    Canvas.SetTop(imgFont, (mainCanvas.ActualHeight - tempImage.Height) / 2);
                }
            }
            catch (Exception e)
            {

                Console.WriteLine("ERROR:绘制文字出错，message：{}",e.Message);
            }
        }

        private void rFontColor_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
        }

        private void cbFontName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (fontItem != null)
            {
                fontItem.FontName = cbFontName.Text;
                DrawText();
            }
        }

        private void tbFontText_TextChanged(object sender, TextChangedEventArgs e)
        {
            GetFontItem(tbFontText.Text);
            DrawText();
        }

        private void numFontSize_ValueChanged(object sender, TextChangedEventArgs e)
        {
            if (fontItem != null)
            {
                fontItem.FontSize = "".Equals(numFontSize.Text) ? 1 : int.Parse(numFontSize.Text);
                DrawText();
            }
        }
    }
}
